import { ref, onMounted } from 'vue';
import { getHotSearchApi } from '#/api/search';

/**
 * 热门搜索功能的组合式函数
 */
export function useHotSearch() {
  const hotSearchList = ref<string[]>([]);
  const loading = ref(false);
  const error = ref<string>('');

  // 获取热门搜索数据
  const fetchHotSearch = async () => {
    try {
      loading.value = true;
      error.value = '';

      const result = await getHotSearchApi();
      hotSearchList.value = result || [];
    } catch (err) {
      console.warn('获取热门搜索失败:', err);
      error.value = '网络请求失败';
      hotSearchList.value = [];
    } finally {
      loading.value = false;
    }
  };

  // 组件挂载时自动获取数据
  onMounted(() => {
    fetchHotSearch();
  });

  return {
    hotSearchList,
    loading,
    error,
    fetchHotSearch,
    refetch: fetchHotSearch
  };
}
